<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>宿舍查询显示</title>
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/popup.css" type="text/css" />
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/style.css" type="text/css" />
		<style>
			/* Clear Fix */
			
			.clearfix:after
			{
			content:".";
			display:block;
			height:0;
			clear:both;
			visibility:hidden;
			}
			.clearfix
			{
			display:inline-block;
			}
			/* Hide from IE Mac */
			.clearfix {display:block;}
			/* End hide from IE Mac */
			/* end of clearfix */
		</style>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/sorttable.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/popup.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/apartmentViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dormitoryViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/studentInDormitoryInfoViewAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/dWRCollegeMajorAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/hallAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>			
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/page.js'></script>
		<script type='text/javascript'>
			$j(function(){
				tbCSS();
			})
		</script>
		<script type="text/javascript">
			//全局变量
			var stuCache={};
			var inWhichDorm; //记录点击公寓时，该公寓的displayname，方便知道现实的学生是哪个公寓的
			var DMS_ROOT = '<%=request.getContextPath()%>';
			var dormitoryConcreteFunc = new Array(); //宿舍具体功能数组
			
			
			
			//============页面初始化开始=============
			$j(function(){
				getAllDormitoryConcreteFunction();
				showAllCollege($j("#dsv_studentcollege").get(0));
				setApartList($j("#dsv_apartment").get(0));
				
				if(${apartmentid} != -1){
					for(var a = 1 ;a< $j("#dsv_apartment ").get(0).options.length;a++){
						if($j("#dsv_apartment").get(0).options[a].value == ${apartmentid} ){
							$j("#dsv_apartment").get(0).options[a].selected = "selected";
							apartmentChange($j("#dsv_apartment").get(0));
							break;
						}
					}
					dormSearch();
				}
					
			});
			//--------------页面初始化结束----------
			
			
			//============自定义函数开始=============
			
			//查询宿舍所有具体功能名称
			function getAllDormitoryConcreteFunction(){
				dormitoryViewAction.getAllDormitoryConcreteFunction(function (funcList){
					dormitoryConcreteFunc = funcList;
					for(var i=0;i<funcList.length;i++){
						$j("#dsv_studentdormitorytype").append("<option value='"+funcList[i][0]+"'>"+funcList[i][1]+"</option>");
					}
				});
			}
			//显示所有的学院
			function showAllCollege(collegeObj){
				DWREngine.setAsync(false);  //dwr同步
				dWRCollegeMajorAction.getAllCollegeInfo(function (collegeList){
						if(null == collegeList){
							return;
						}
						var collegeInfo =collegeList;
						var s =collegeObj;
						 	DWRUtil.removeAllOptions(s);  
							s.options[0] = new Option("不限学院",'0');
							//alert(collegeList[0].collegeinfoname)
						for ( var i = 1; i < collegeList.length+1; i++) {
							var opt = new Option(collegeList[i-1].collegeinfoname,
									collegeList[i-1].collegeinfoid);
							s.options[i] = opt;
						}
				});
				DWREngine.setAsync(true);  //恢复dwr异步
			}
			
			//显示所有的公寓
			function setApartList(obj) {
				DWREngine.setAsync(false);  //dwr同步
				apartmentViewAction.getAllApartInfo(function (aptValue){
					aptInfo =aptValue;
					apartamount = aptInfo.length; //给公寓总条数赋值
					var s = $(obj);
				 	DWRUtil.removeAllOptions(s); 
					s.options[0] = new Option("不限公寓",'0');
					for ( var i = 1; i < aptValue.length+1; i++) {
						var opt = new Option(aptValue[i-1].apartmentname,aptValue[i-1].apartmentid);
						s.options[i] = opt;
					}
				});
				DWREngine.setAsync(true);  //恢复dwr异步
			}
			//公寓变动时触发，apartment是公寓select元素,floor是楼层select元素
			function apartmentChange(apartment){
	
				//apartmentid = $("aptName").options[$("aptName").selectedIndex].value; //给apartmentid赋值
				apartmentid = apartment.options[apartment.selectedIndex].value;
				//取到了对应apartmentid的楼层数目
				var aprt_floorno;
							var aprt_name;
							for(var m = 0; m<apartamount;m++){
								if(aptInfo[m].apartmentid ==apartmentid){
								aprt_floorno = aptInfo[m].apartmentfloornum;
								aprt_name = aptInfo[m].apartmentname;
								break;
								}
							}
							var sf = $j("#dsv_floor").get(0);
							DWRUtil.removeAllOptions(sf);  
							sf.options[0] = new Option("不限楼层",'0');
							if(aprt_name == "七号楼"){
							for ( var j = 1; j < (parseInt(aprt_floorno)-1); j++) {
								var optt = new Option((j+2)+"层",(j+2));
								sf.options[j] = optt;
							}
							}
							else if(aprt_name == "三号楼"){
							for ( var j = 1; j < (parseInt(aprt_floorno)); j++) {
								var optt = new Option((j+1)+"层",(j+1));
								sf.options[j] = optt;
							}
							}
							else{
							for ( var j = 1; j < (parseInt(aprt_floorno)+1); j++) {
								var optt = new Option(j+"层",j);
								sf.options[j] = optt;
							}
							
							}
			}
				
			function showdormitory(dormitoryidandname){
			
				//查看宿舍的图片显示方式
				if (document.getElementById("tmpDormGrid").value != 0)
				{
				   var tmp = document.getElementById("tmpDormGrid").value;
				   if(document.getElementById(tmp)){
				   		document.getElementById(tmp).style.border='1px solid white';
				   }
				}
				document.getElementById("tmpDormGrid").value=dormitoryidandname;
				document.getElementById(dormitoryidandname).style.border='1px solid black';
				
				//显示宿舍的基本信息
				var dormitoryid = dormitoryidandname.split("_")[0];
				dormitoryViewAction.getDormByDormId(dormitoryid,function(data){
					var dorm = eval('('+data+')');
					//alert(data);
					$j("#dormview_dormid").get(0).innerHTML = dorm.dormitoryid;
					$j("#dormview_dormtype").get(0).innerHTML = dorm.dormitorybuildingtype;
					
					var seleObject = document.createElement("select");
					
					if(dormitoryConcreteFunc.length == 0){
						getAllDormitoryConcreteFunction();
					}
					for(var a=0;a<dormitoryConcreteFunc.length;a++){
						seleObject.options[a] = new Option(dormitoryConcreteFunc[a][1],dormitoryConcreteFunc[a][0]);
					}
					$j("#dormview_dormconcretefunction").html(seleObject);
					for(var a = 0 ;a<seleObject.options.length;a++){
						if(seleObject.options[a].value == dorm.dormitoryconcretefuncid){
							seleObject.options[a].selected = "selected";
						}
					}
					
					$j(seleObject).change(function (){
						$j(seleObject).parent().find("input").remove();
						$j(seleObject).after("<input type='button' onclick='saveDormAttrb(this,0)' value='保存'>");
					});
							
					
					$j("#dormview_dormdirection").get(0).innerHTML = dorm.dormitorydirection;
					$j("#dormview_dormsquare").get(0).innerHTML = (dorm.dormitorysquare==-1)?"":dorm.dormitorysquare;
					$j("#dormview_dormlevel").get(0).innerHTML = dorm.dormitorylevel;
					$j("#dormview_dormmisc").get(0).innerHTML = dorm.dormitorymisc;
				});
			}
			
			// 点击宿舍查看入住学生
			function showstudentbydormitoryid(dormitoryidandname){
				var dormitoryid = dormitoryidandname.split("_")[0];
				//alert(dormitoryid)
				inWhichDorm = dormitoryidandname.split("_")[1]; //全局变量赋值 
				studentInDormitoryInfoViewAction.listStudentInfoInSpecDorm(dormitoryid,showstudentcallback);
			}
			var showstudentcallback = function showstudentcb(showstulist){
				if($('stuinfolistdiv').style.visibility == 'hidden'){
					$('stuinfolistdiv').style.visibility = 'visible';
				}
				if($('hr_id').style.display == "none"){
						$('hr_id').style.display = "";
					}
				if(showstulist==null){ //未入住学生的宿舍给予提示
				 DWRUtil.removeAllRows('ttb', { filter:function(tr) {
				      return (tr.id != "pattern");
					    }});
				if($('student_basic_info_search').style.visibility == 'visible'){
						$('student_basic_info_search').style.visibility = 'hidden';
					}
					if($('div_basicInfo').style.display == ""){
						$('div_basicInfo').style.display = "none";
					}
					
				return;
			}
			
				DWRUtil.removeAllRows('ttb', { filter:function(tr) {
				      return (tr.id != "pattern");
					    }});
				var st,id;
				 for (var i =0; i < showstulist.length ; i++) {
				 st = showstulist[i];
				 id = st[1]; //编号 
				 dwr.util.cloneNode("pattern", { idSuffix:id });
			     dwr.util.setValue("tablestudentname"+id, st[2]);
			     dwr.util.setValue("tablestudentgender"+id, st[8]);
			     dwr.util.setValue("tablestudentno"+id, st[1]);
			     if(typeof(st[6])=="object"){
				 dwr.util.setValue("tablestudentclass"+id,"");  
				 }else{
			 	 dwr.util.setValue("tablestudentclass"+id,st[6]);  
				 } 
				 if(typeof(st[36])=="object"){
				 dwr.util.setValue("tablestudentcollegeinfo"+id,"");  
				 }else{
			 	 dwr.util.setValue("tablestudentcollegeinfo"+id,st[36]);  
				 }
				 if(typeof(st[37])=="object"){
				 dwr.util.setValue("tablestudentmajorinfo"+id,"");  
				 }else{
			 	 dwr.util.setValue("tablestudentmajorinfo"+id,st[37]);  
				 }
				 dwr.util.setValue("tableintohall"+id,"移入大厅");  
			     dwr.util.setValue("cbx" + id, st[0]);
			 	 $("pattern" + id).style.display= "";
			 	 stuCache[id] = st;
			   }
			   var str = showstulist[0];
			   showinfo("cbx"+str[1]);
			   tbCSS();
			}
			
			function showinfo(eleid){
			var student_number = eleid.substring(3);
			$('student_info').value = student_number;
			stu_BasicInfo();
			$('hr_id').style.display = "";
			$('div_basicInfo').style.display = "";
			$('student_basic_info_search').style.display = "none";
			}
			
			//编辑dorm信息
			
			function editDorm(obj,attrName,size){
				try{   
					if(!document.getElementById("edited"+attrName)){
						obj.innerHTML ="<input type='text' id='edited"+attrName+"' value='" + obj.innerHTML + "' size='"+size+"' />"+ "<input type='button' onclick='saveDormAttrb(this,1)' value='保存'>";
					}
				}catch(e){
				
				} 
			}
			//保存dorm信息
			//tag 为1表示text ,0表示select
			function saveDormAttrb(obj,tag){
				
				var atrribValue;
				var atrribKey;
				if(tag == 0){
					atrribKey = $j(obj).parent().find("select").get(0).id;
					atrribValue = $j(obj).parent().find("select option:selected").val();
				}else {
					atrribKey = $j(obj).parent().find("input").get(0).id;
					atrribValue = $j(obj).parent().find("input").get(0).value;
				}
				var patrn;
				if(atrribKey == "editedroomtype"){
					patrn=/^[0-9]{1,2}$/; 
					if (!patrn.exec(atrribValue)){
						alert("输入错误");
						return;
					}
				}
				if(atrribKey == "editedsquare"){
					patrn=/^[0-9]{1,2}$/; 
					if (!patrn.exec(atrribValue)){
						alert("输入错误");
						return;
					}
				}
				obj.value = "保存中";
				obj.disabled = true;
				
				dormitoryViewAction.updateDormInfo(atrribKey,atrribValue,$j("#dormview_dormid").get(0).innerHTML,function(data){
					if(data == "success"){
						//alert("成功");
						if(tag == 0){
							$j(obj).remove();
						}else {
							$j(obj).parent().html($j(obj).parent().find("input").get(0).value);
							$j(obj).remove();
						}
						//dormSearch();
					}else{
						alert("操作失败!");
						obj.disabled = false;
					}
				
				});
			}
			//宿舍高级查询
			function dormSearch(){
				var dsv_sql = "select dormitoryid,dormitoryname,dormitoryfunction,dormitoryconcretefuncid,"
					+"dormitorylodgetype,dormitorysex,count(dormitorybedid) as usedbed from advancedDormSearch where 1=1";
				
				
				//----楼条件
				if($j("#dsv_apartment").get(0).options.selectedIndex != 0){
					dsv_sql += " and apartmentid = " + $j("#dsv_apartment option:selected").get(0).value;
				}
				
				//----层条件
				if($j("#dsv_floor").get(0).options.length != 0 && $j("#dsv_floor").get(0).options.selectedIndex != 0){
					dsv_sql += " and  dormitoryfloorno = " + $j("#dsv_floor option:selected").get(0).value;
				}
				//----性别
				if($j("#dsv_dormitorygender").get(0).options.selectedIndex != 0){
					if($j("#dsv_dormitorygender").get(0).options.selectedIndex == 1){
						dsv_sql += " and dormitorysex = 1";
					}else{
						dsv_sql += " and dormitorysex = 0";
					}
				}
				//----学院
				if($j("#dsv_studentcollege").get(0).options.selectedIndex != 0){
					dsv_sql += " and collegeinfoid = " + $j("#dsv_studentcollege option:selected").val();
				}
				//-----入学年份
				if($j("#dsv_studentenrollyear").get(0).options.selectedIndex != 0){
					//alert($j("#dsv_studentenrollyear").get(0).options.selectedIndex);
					dsv_sql += " and studentenrollyear = '" + $j("#dsv_studentenrollyear option:selected").html() +"'";
				}
				//-----学生类型
				if($j("#dsv_studenttype").get(0).options.selectedIndex != 0){
					dsv_sql += " and studenttype = '" + $j("#dsv_studenttype option:selected").html() +"'";
				}
				//------住宿类型
				if($j("#dsv_dormitoryfunction").get(0).options.selectedIndex != 0){
					dsv_sql += " and dormitoryfunction = " + $j("#dsv_dormitoryfunction option:selected").val();
				}
				//--------具体功能
				if($j("#dsv_studentdormitorytype").get(0).options.selectedIndex != 0){
					dsv_sql += " and dormitoryconcretefuncid = " + $j("#dsv_studentdormitorytype option:selected").val();
				}
				dsv_sql+=" group by dormitoryid";
				//----是否空闲
				if($j("#dsv_dormitoryblank").get(0).options.selectedIndex != 0){
					//空闲时
					if($j("#dsv_dormitoryblank").get(0).options.selectedIndex == 1){
						dsv_sql+=" having usedbed <> dormitorylodgetype";
					}else{  //非空闲时
						dsv_sql+=" having usedbed = dormitorylodgetype";
					}
				}
				
				dsv_sql+=" order by dormitorysort";
				alert(dsv_sql);
				
				var sql = dsv_sql;
				var start = 0;
				var offset = 36;
				var total = 0;
				DWREngine.setAsync(false);  
				dormitoryViewAction.getAdvancedDormSearchTotalNumber(sql,function(data){
					total = data;
					//alert(total)
				});
				
				page("pageShow",sql,start,offset,total,function(c){
						//alert("#"+c);
						$j('#dormShow').empty();
						//alert(total)
						if(total == 0){
								alert("查询结果为空");
								return ;
						}
						dormitoryViewAction.advancedDormSearch(c,function(data){
							for(var a=0;a<data.length;a++){
								var datarow = data[a];
								var dormPicName;  // =性别（男1女0）+房间可住人数+房间已住人数
								
								
								if(datarow[2] == 1){
									dormPicName = ""+datarow[5]+datarow[4]+datarow[6];
									//alert(dormPicName)
									
								}else {
									dormPicName = "weizhi";
									if(dormitoryConcreteFunc.length == 0){
										getAllDormitoryConcreteFunction();
									}
									for(var i=0 ;i<dormitoryConcreteFunc.length;i++){
										if(datarow[3] == dormitoryConcreteFunc[i][0]){
											dormPicName = dormitoryConcreteFunc[i][2];
											break;
										}
									}
								}
							
								dormhtml='<div  style="float:left" >'
										+ 	'<div>'
										+		'<img src="<%=request.getContextPath()%>/images/dormpic/'+dormPicName+'.jpg"'
										+ 		' id="'+datarow[0]+'_'+datarow[1]+'" style="border:1px solid white"'
										+ 		'	onclick="javascript:showstudentbydormitoryid(this.id),showdormitory(this.id);" /></div>'
										+ 	' <div class="dormitoryinfo" >'+datarow[1]+'</div>'
										+ '</div>'
								$j('#dormShow').append(dormhtml);
							}
						});
					});
				
				DWREngine.setAsync(true);
				$j("#dormview_dormitoryinfo").get(0).style.display="block";
			}
			//宿舍类型下拉框变化的时候,具体功能下拉框联动
			function dsv_dormitoryfunctionChange(obj){
				var tmp = $j(obj).find("option:selected").val();
				$j("#dsv_studentdormitorytype").empty();
				$j("#dsv_studentdormitorytype").append("<option>具体功能</option>");
				for(var i=0;i<dormitoryConcreteFunc.length;i++){
					if(obj.selectedIndex == 0){
						$j("#dsv_studentdormitorytype").append("<option value='"+dormitoryConcreteFunc[i][0]+"'>"+dormitoryConcreteFunc[i][1]+"</option>");					
					}else if(dormitoryConcreteFunc[i][3] == tmp){
						$j("#dsv_studentdormitorytype").append("<option value='"+dormitoryConcreteFunc[i][0]+"'>"+dormitoryConcreteFunc[i][1]+"</option>");
					}
				}
			}
			//移入大厅
			function dsv_intohall(obj){
				if(!confirm("确定移入大厅?")){
					return;
				}
				hallAction.intoHall($j(obj).parent().parent().find("td span").html(),function(message){
					if("移入大厅成功" == message){
						$j(obj).parent().parent().remove();
					}else{
						alert(message);
					}
					
				});
			}
//--------------自定义函数结束-----------
</script>
	</head>
	<body  style="margin:0px;padding:0px;">
		<s:hidden name="tmpDormGrid" value="0" />
		<div id="page_container" style="overflow-y:visible">
			<div class="divground">
						<select name="apartment" id="dsv_apartment" style="width:100px" onchange="apartmentChange(this)" >
						</select>
						<select id="dsv_floor" name="floor" style="width:100px">
						</select>
						
						<select id="dsv_dormitoryblank" name="dormitoryblank"  style="width:100px">
							<option>空闲或非空闲</option>
							<option>空闲</option>
							<option>非空闲</option>
						</select>
						<select id="dsv_dormitorygender" name="dormitorygender" style="width:100px" >
							<option>不限性别</option>
							<option>男</option>
							<option>女</option>
						</select>
						<select id="dsv_studentcollege" name="studentcollege" style="width:100px">
						</select>
						<select id="dsv_studentenrollyear" name="studentenrollyear" style="width:100px">
							<option>学生入学年份</option>
							<option>2002</option>
							<option>2003</option>
							<option>2004</option>
							<option>2005</option>
							<option>2006</option>
							<option>2007</option>
							<option>2008</option>
							<option>2009</option>
							<option>2010</option>
							<option>2011</option>
							<option>2012</option>
							<option>2013</option>
							<option>2014</option>
							<option>2015</option>
							<option>2016</option>
						</select>
						<select id="dsv_studenttype" name="dsv_studenttype" style="width:100px">
							<option>学生类型</option>
							<option>本科</option>
							<option>硕士</option>
							<option>博士</option>
							<option>专升本</option>
							<option>定向</option>
						</select>
						<select id="dsv_dormitoryfunction" onchange="dsv_dormitoryfunctionChange(this)">
							<option value="-1">宿舍类型</option>
							<option value="1">住宿</option>
							<option value="0">非住宿</option>
						</select>
						<select id="dsv_studentdormitorytype">
							<option>具体功能</option>
							
						</select>
						<input type="button" value="查询" onclick="dormSearch()"/>
				
				</div>
				<br/>
				<div id="dormShow" >
				</div>
				<div id="pageShow"  class="table_common">
				</div>
				<hr style="width: 90%" />
				<!--  暂时去掉-->
				<div style="display:none">
					<%@ include file="dormInfo.jsp"%>
				</div>
				<div id="dormview_studentinfo">
					<div id="stuinfolistdiv" style="visibility: hidden;">
						<table id="stuinfolistdiv_table" class="sortable table_common table_list_style" rowclass="row_odd,row_even" style="width: 600px;">
							<tr>
									<th style="width: 10%">学号</th>
									<th style="width: 10%">姓名</th>
									<th style="width: 10%">性别</th>
									<th style="width: 15%">班级</th>
									<th style="width: 20%">学院</th>
									<th style="width: 20%">专业</th>
									<th style="width: 15%">操作</th>
									<th style="display:none"></th>
							</tr>
							<tbody id="ttb">
								<!-- 隐藏的项目 onclick="test(this.id)-->
								<tr id="pattern" style="display: none;"
									onclick="javascript:showinfo(this.id.substr(4))">
									<td>
										<div id="tablestudentno"></div>
									</td>
									<td height="18">
										<div id="tablestudentname"></div>
									</td>
									<td>
										<div id="tablestudentgender"></div>
									</td>
									<td height="18">
										<div id="tablestudentclass"></div>
									</td>
									<td>
										<div id="tablestudentcollegeinfo"></div>
									</td>
									<td>
										<div id="tablestudentmajorinfo"></div>
									</td>
									<td>
										<button id="tableintohall" onclick="dsv_intohall(this)"></button>
									</td>
									 
									<td style="display:none">
										<span id="cbx"  ></span>
									</td>
									 
								</tr>
							</tbody>
						</table>
					</div>
					<hr style="display: none;width: 90%" id="hr_id" />
					<!-- 学生基本信息tab页显示开始 -->
					<div style="display: none;" id="div_basicInfo">
						<jsp:include page="/allperson/basicstudentinfo.jsp"></jsp:include>
					</div>
					<!-- 学生基本信息tab页显示结束 -->
				</div>
		</div>
	</body>
</html>
